<?php

namespace Biz\service\brand;

use Biz\model\brandActivity\BlindBoxActivity as mBlindBoxActivity;
use Biz\model\brandActivity\BlindBoxLottery as mBlindBoxLottery;
use Biz\model\brandActivity\BlindBoxLotteryConsignee as mBlindBoxLotteryConsignee;
use Biz\model\brandActivity\BlindBoxActivityPrize as mBlindBoxActivityPrize;
use Biz\model\trade\ShopCourier as mShopCourier;
use Biz\pockets\constInterface\BlindBoxConfig;
use Biz\service\NewProduct\NewProductWinnerListService;
use Doraemon\service\User;
use Hhz\AdministrativeRegion\Region;
use Doraemon\tools\ImgUrl;

class AdmBlindBoxLottery implements BlindBoxConfig
{
    /**
     * 中奖名单列表
     * @param array $params
     * @return array
     */
    public static function getWinnerList(array $params): array
    {
        $result = ['total' => 0, 'list' => [], 'activity_info' => []];
        $page = $params['page'];
        $pageSize = $params['page_size'];
        $activityId = $params['activity_id'];
        if (!$activityId) {
            return $result;
        }

        // 查询活动信息
        $result['activity_info'] = mBlindBoxActivity::getInfoById($activityId) ?: [];

        // 根据活动ID查询中奖名单,分页
        $where = [
            'activity_id' => $activityId,
            'lottery_state[!]' => mBlindBoxLottery::LOTTERY_STATE_DEL,
            'ORDER' => ['addtime' => 'DESC'],
            'LIMIT' => [($page - 1) * $pageSize, $pageSize],
        ];
        $blindBoxLotteryCollection = mBlindBoxLottery::selectByWhere($where) ?: [];
        if (!$blindBoxLotteryCollection) {
            return $result;
        }

        unset($where['LIMIT']);
        // 总条数
        $count = count(mBlindBoxLottery::selectByWhere($where));
        if (!$count) {
            return $result;
        }

        $result['total'] = $count;

        // 查询用户信息
        $userInfos = NewProductWinnerListService::handleData(User::getBatchBasicUserInfo(array_column($blindBoxLotteryCollection, 'uid'), 0), 'uid');

        // 查询奖品信息
        $activityPrizeList = mBlindBoxActivityPrize::getActivityPrizeList([$activityId]);

        // 查询收货地址信息
        $blindBoxLotteryConsigneeCollection = mBlindBoxLotteryConsignee::getByLotteryIds(array_column($blindBoxLotteryCollection, 'id'));
        $blindBoxLotteryConsigneeMap = NewProductWinnerListService::handleData($blindBoxLotteryConsigneeCollection, 'lottery_id');

        // 查询物流信息
        $courierMap = [];
        if ($blindBoxLotteryConsigneeCollection) {
            $courierIds = array_values(array_unique(array_column($blindBoxLotteryConsigneeCollection, 'courier_id')));
            $courierMap = NewProductWinnerListService::handleData(mShopCourier::getByCourierIds($courierIds), 'courier_id');
        }

        foreach ($blindBoxLotteryCollection as $value) {
            // 用户信息
            $value['user_info'] = $userInfos[$value['uid']] ?? [];
            if ($value['user_info']) {
                $value['nickName'] = $value['user_info']['nick'] ?? '';
            }

            $courier = [];
            $blindBoxLotteryConsignee = $blindBoxLotteryConsigneeMap[$value['id']] ?: [];
            if ($blindBoxLotteryConsignee) {
                $courier = $courierMap ? $courierMap[$blindBoxLotteryConsignee['courier_id']] : [];
            }

            $card = [];
            $limitCard = [];
            $coupon = [];
            // 奖品信息
            $prizeList = $activityPrizeList[$activityId] ?: [];
            foreach ($prizeList as $prizeInfo) {
                if (in_array($prizeInfo['prize_type'], [mBlindBoxActivityPrize::PRIZE_TYPE_JD_CARD])) {
                    $card[] = $prizeInfo['prize_value'];
                } else if (in_array($prizeInfo['prize_type'], [mBlindBoxActivityPrize::PRIZE_TYPE_SHOP_COUPON, mBlindBoxActivityPrize::PRIZE_TYPE_GOOD_COUPON])) {
                    $coupon[] = $prizeInfo['prize_value'];
                } else if (in_array($prizeInfo['prize_type'], [mBlindBoxActivityPrize::PRIZE_TYPE_LIMIT_CARD])) {
                    $img = ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $prizeInfo['prize_value'], ImgUrl::IMAGE_SIZE_OP_ORI);
                    $limitCard[] = $img;
                }
            }

            $value['card'] = $card;
            $value['limit_card'] = $limitCard;
            $value['coupon'] = $coupon;
            $value['basic_id'] = $result['activity_info'] ? $result['activity_info']['basic_id'] : '';

            // 收货信息
            if ($blindBoxLotteryConsignee) {
                $value['consignee_name'] = $blindBoxLotteryConsignee['consignee_name'];
                $value['consignee_mobile'] = $blindBoxLotteryConsignee['consignee_mobile'];
                $value['consignee_mobile_encrypt_id'] = $blindBoxLotteryConsignee['consignee_mobile_encrypt_id'];
                $value['consign_address'] = $blindBoxLotteryConsignee['area_name'] . $blindBoxLotteryConsignee['consignee_address'];
                $value['express_no'] = $blindBoxLotteryConsignee['express_no'];
            }

            // 物流信息
            if ($courier) {
                $value['courier_name'] = $courier['courier_name'];
            }

            $result['list'][] = $value;
        }

        return $result;
    }
}